package org.nbict.iot.task.once.state;

import org.apache.storm.trident.operation.TridentCollector;
import org.apache.storm.trident.state.State;
import org.apache.storm.trident.tuple.TridentTuple;
import org.nbict.iot.persist.pool.DbRepository;
import org.nbict.iot.persist.repo.RedisRepo;
import org.nbict.iot.task.once.Pointer;

import java.util.ArrayList;
import java.util.List;

/**
 * 将车辆上报点进行redis存储
 * Created by songseven on 18/7/10.
 */
public class RunningWinState implements State {

    private Long txid;
    private Long total = 0l;

    public void prepare() {

    }

    @Override
    public void beginCommit(Long txid) {
        this.txid = txid;
    }

    @Override
    public void commit(Long txid) {
        //System.out.println(txid);
    }

    public void updateState(List<TridentTuple> tuples, TridentCollector
            collector) {
        List<Pointer> pointers = new ArrayList<>();
        tuples.forEach(tuple -> {
            Pointer p = (Pointer) tuple.get(0);
            pointers.add(p);
        });

        //size+= pointers.size();
        //System.out.println("size: " + pointers.size() + " total:" + size);
        ((RedisRepo) DbRepository.REDIS.getInstance()).putZSet(pointers);
    }
}
